/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazEliminarUsuario.java
 *
 * Created on 26/10/2011, 01:52:51 AM
 */
package presentacion;

import dominio.Profesor;
import Paneles.PanelGeneral;
import business.AdministradorBo;
import business.EstudianteBO;
import business.ProfesorBo;
import business.UsuarioBO;
import dominio.Administrador;
import dominio.Estudiante;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author SEBASTIAN
 */
public class InterfazEliminarUsuario extends javax.swing.JFrame {

    private ImageIcon icono;
    private Long indiceDoc[];
    private String user[];

    /** Creates new form InterfazEliminarUsuario */
    public InterfazEliminarUsuario() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.EAST);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Eliminar Usuario");

        jLabel1.setText("Nombre de Usuario:");

        jTextField2.setEditable(false);

        jButton1.setText("Eliminar Usuario");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Escoja:", "Estudiante", "Profesor", "Administrador" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Tipo de Usuario");

        jLabel3.setText("Eliminar a:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox2, 0, 224, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 224, Short.MAX_VALUE)))
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        if (jComboBox1.getSelectedIndex() == 1) {
            poblarComboEstudiantes();
            if (jComboBox2.getItemCount() == 1) {
                JOptionPane.showMessageDialog(this, "Estudiantes sin usuario: 0", "No hay estudiantes sin usuario", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if (jComboBox1.getSelectedIndex() == 2) {
            poblarComboProfesores();
            if (jComboBox2.getItemCount() == 1) {
                JOptionPane.showMessageDialog(this, "Profesores sin usuario: 0", "No hay Profesores sin usuario", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if (jComboBox1.getSelectedIndex() == 3) {
            poblarComboAdministradores();
        }


    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

            if (jComboBox1.getSelectedIndex() == 1) {//crear usuario a estudiante
                if (jComboBox2.getItemCount() == 1) {
                    JOptionPane.showMessageDialog(this, "Seleccione Estudiante", "", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    eliminarUsuarioAEstudiante();
                }
            }
            if (jComboBox1.getSelectedIndex() == 2) {//crear usuario a prf
                if (jComboBox2.getItemCount() == 1) {
                    JOptionPane.showMessageDialog(this, "Seleccione Profesor", "", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    eliminarUsuarioAProfesor();
                }
            }
            if (jComboBox1.getSelectedIndex() == 3) {//crear usuario a ad
                if (jComboBox2.getItemCount() == 1) {
                    JOptionPane.showMessageDialog(this, "Seleccione Administrador", "", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    eliminarUsuarioAAdministrador();
                }

            }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        jTextField2.setText(user[jComboBox2.getSelectedIndex() - 1]);
    }//GEN-LAST:event_jComboBox2ActionPerformed
private void poblarComboEstudiantes() {
        EstudianteBO ebo = new EstudianteBO();
        Collection<Estudiante> e = ebo.encontrarEstudiantesConUsuario();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Seleccione"}));
        Iterator<Estudiante> itp = e.iterator();
        if (e.size() > 0) {
            indiceDoc = new Long[e.size()];
            user = new String[e.size()];
        }
        int i = 0;
        while (itp.hasNext()) {
            Estudiante p = itp.next();
            indiceDoc[i] = p.getDocEstudiante();
            user[i]=p.getNombreUsuario();
            jComboBox2.addItem(p.getApellidoEst() + " " + p.getNombreEst() + " [Doc: " + p.getDocEstudiante() + "]");
            i++;
        }
    }
    private void poblarComboAdministradores() {
        AdministradorBo pbo = new AdministradorBo();
        Collection<Administrador> p = pbo.encontrarAdministradores();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Seleccione"}));
        Iterator<Administrador> itp = p.iterator();
        if (p.size() > 0) {
            user = new String[p.size()];
        }
        int i = 0;
        while (itp.hasNext()) {
            Administrador actual = itp.next();
            user[i] = actual.getNombreUsuario();
            jComboBox2.addItem(actual.getApellidoAdm() + " " + actual.getNombreAdm());
            i++;
        }

    }
    private void poblarComboProfesores() {
        ProfesorBo pbo = new ProfesorBo();
        Collection<Profesor> p = pbo.encontrarProfesoresConUsuario();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Seleccione"}));
        Iterator<Profesor> itp = p.iterator();
        if (p.size() > 0) {
            indiceDoc = new Long[p.size()];
            user = new String[p.size()];
        }
        int i = 0;
        while (itp.hasNext()) {
            Profesor profeActual = itp.next();
            indiceDoc[i] = profeActual.getDocProfesor();
            user[i]=profeActual.getNombreUsuario();
            jComboBox2.addItem(profeActual.getApellidoProfesor() + " " + profeActual.getNombreProfesor() + " [Doc: " + profeActual.getDocProfesor() + "]");
            i++;
        }
    }
    private void eliminarUsuarioAEstudiante() {
        EstudianteBO ebo = new EstudianteBO();
        ebo.asignarUsuarioAEstudiante(indiceDoc[jComboBox2.getSelectedIndex() - 1], null);
        UsuarioBO ubo = new UsuarioBO();
        ubo.borrarUsuario(user[jComboBox2.getSelectedIndex() - 1]);
        poblarComboEstudiantes();
        operacionFinal();
    }
    private void eliminarUsuarioAProfesor() {
        ProfesorBo ebo = new ProfesorBo();
        ebo.asignarUsuarioAProfesor(indiceDoc[jComboBox2.getSelectedIndex() - 1], null);
        UsuarioBO ubo = new UsuarioBO();
        ubo.borrarUsuario(user[jComboBox2.getSelectedIndex() - 1]);
        poblarComboProfesores();
        operacionFinal();
    }
     private void eliminarUsuarioAAdministrador() {
        AdministradorBo ebo = new AdministradorBo();
        ebo.borrarAdministrador(user[jComboBox2.getSelectedIndex() - 1]);
        UsuarioBO ubo = new UsuarioBO();
        ubo.borrarUsuario(user[jComboBox2.getSelectedIndex() - 1]);
        operacionFinal();
    }
     public void operacionFinal() {
        JOptionPane.showMessageDialog(this, "El usuario fue eliminado satisfactoriamente", "Operacion Existosa", JOptionPane.INFORMATION_MESSAGE);
        jTextField2.setText("");

    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazEliminarUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
